package org.tinaelle.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.tinaelle.ServiceLocator;
import org.tinaelle.client.utils.Utilities;
import org.tinaelle.common.criteria.AdScreenSearchCriteria;
import org.tinaelle.common.criteria.AdTableSearchCriteria;
import org.tinaelle.common.criteria.AdTreeNodeSearchCriteria;
import org.tinaelle.common.dto.AdObjectDTO;
import org.tinaelle.common.dto.AdScreenDTO;
import org.tinaelle.common.dto.AdTableDTO;
import org.tinaelle.common.dto.AdTreeDTO;
import org.tinaelle.common.dto.AdTreeNodeDTO;
import org.tinaelle.common.dto.SearchDataResultDTO;
import org.tinaelle.servlet.utils.json.JSONArray;
import org.tinaelle.servlet.utils.json.JSONException;
import org.tinaelle.servlet.utils.json.JSONObject;

public class JSONServlet extends HttpServlet {

	/**
     * 
     */
	private static final long	serialVersionUID	= -5390482145000082448L;

	@Override
	public void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
	        IOException {
		doPost( req, resp );
	}

	@Override
	public void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
	        IOException {
		resp.setContentType( "text/json; charset=iso-8859-1" );
		resp.addHeader( "Cache-Control", "NO-CACHE" );
		resp.addHeader( "Pragma", "NO-CACHE" );
		resp.addHeader( "EXPIRES", "-1" );

		final PrintWriter out = resp.getWriter();

		if ( req.getServletPath().indexOf( "getComboForTree.jservlet" ) != -1 ) {
			final String adTreeId = req.getParameter( "adTreeId" );
			final String query = req.getParameter( "query" );
			req.getParameter( "filterCol" );
			final String adTreeNodeId = req.getParameter( "adTreeNodeId" );
			SearchDataResultDTO result;

			if ( !Utilities.isBlankOrNull( adTreeNodeId ) ) {
				final AdTreeNodeDTO treeNode = ServiceLocator.instance().getAdTreeService().getTreeNodeById(
				        new Integer( adTreeNodeId ) );
				result = new SearchDataResultDTO();
				result.setTotalResult( 1 );
				final ArrayList<AdObjectDTO> tmpList = new ArrayList<AdObjectDTO>( 1 );
				tmpList.add( treeNode );
				result.setResult( tmpList );
			} else {
				final AdTreeDTO adTreeDTO = new AdTreeDTO();
				adTreeDTO.setAdTreeId( new Integer( adTreeId ) );

				final AdTreeNodeSearchCriteria searchCriteria = new AdTreeNodeSearchCriteria();
				searchCriteria.setName( query );
				searchCriteria.setAdTreeId( new Integer( adTreeId ) );
				result = ServiceLocator.instance().getAdTreeService().findByCriteria( searchCriteria );
			}
			final Collection<AdObjectDTO> resultCol = result.getResult();

			final JSONObject o = new JSONObject();
			try {
				o.put( "totalResult", result.getTotalResult() );
				final JSONArray listOfNodes = new JSONArray();
				for ( final Iterator<AdObjectDTO> i = resultCol.iterator(); i.hasNext(); ) {
					final AdTreeNodeDTO node = ( AdTreeNodeDTO ) i.next();
					final JSONObject oneNode = new JSONObject();
					oneNode.put( "adTreeNodeId", node.getAdTreeNodeId() );
					oneNode.put( "title", node.getName() );
					listOfNodes.put( oneNode );
				}
				o.put( "list", listOfNodes );
			} catch ( final JSONException e ) {
				e.printStackTrace();
			}

			out.print( o.toString() );
		} else if ( req.getServletPath().indexOf( "getComboForScreen.jservlet" ) != -1 ) {
			final String query = req.getParameter( "query" );
			req.getParameter( "filterCol" );
			final String adScreenId = req.getParameter( "adScreenId" );
			SearchDataResultDTO result;

			if ( !Utilities.isBlankOrNull( adScreenId ) ) {
				final AdScreenDTO screen = ServiceLocator.instance().getAdWindowService().getScreenById(
				        new Integer( adScreenId ) );
				result = new SearchDataResultDTO();
				result.setTotalResult( 1 );
				final ArrayList<AdObjectDTO> tmpList = new ArrayList<AdObjectDTO>( 1 );
				tmpList.add( screen );
				result.setResult( tmpList );
			} else {
				final AdScreenSearchCriteria searchCriteria = new AdScreenSearchCriteria();
				searchCriteria.setName( query );

				result = ServiceLocator.instance().getAdWindowService().findByCriteria( searchCriteria );
			}
			final Collection<AdObjectDTO> resultCol = result.getResult();

			final JSONObject o = new JSONObject();
			try {
				o.put( "totalResult", result.getTotalResult() );
				final JSONArray listOfNodes = new JSONArray();
				for ( final Iterator<AdObjectDTO> i = resultCol.iterator(); i.hasNext(); ) {
					final AdScreenDTO screen = ( AdScreenDTO ) i.next();
					final JSONObject oneNode = new JSONObject();
					oneNode.put( "adScreenId", screen.getAdScreenId() );
					oneNode.put( "name", screen.getName() );
					listOfNodes.put( oneNode );
				}
				o.put( "list", listOfNodes );
			} catch ( final JSONException e ) {
				e.printStackTrace();
			}

			out.print( o.toString() );
		} else if ( req.getServletPath().indexOf( "getComboForTable.jservlet" ) != -1 ) {
			final String query = req.getParameter( "query" );
			req.getParameter( "filterCol" );
			final String adTableId = req.getParameter( "adTableId" );
			SearchDataResultDTO result;

			if ( !Utilities.isBlankOrNull( adTableId ) ) {
				final AdTableDTO table = ServiceLocator.instance().getAdSystemService().getTableById(
				        new Integer( adTableId ) );
				result = new SearchDataResultDTO();
				result.setTotalResult( 1 );
				final ArrayList<AdObjectDTO> tmpList = new ArrayList<AdObjectDTO>( 1 );
				tmpList.add( table );
				result.setResult( tmpList );
			} else {
				final AdTableSearchCriteria searchCriteria = new AdTableSearchCriteria();
				searchCriteria.setName( query );

				result = ServiceLocator.instance().getAdSystemService().findByCriteria( searchCriteria );
			}
			final Collection<AdObjectDTO> resultCol = result.getResult();

			final JSONObject o = new JSONObject();
			try {
				o.put( "totalResult", result.getTotalResult() );
				final JSONArray listOfNodes = new JSONArray();
				for ( final Iterator<AdObjectDTO> i = resultCol.iterator(); i.hasNext(); ) {
					final AdTableDTO table = ( AdTableDTO ) i.next();
					final JSONObject oneNode = new JSONObject();
					oneNode.put( "adTableId", table.getAdTableId() );
					oneNode.put( "name", table.getName() );
					listOfNodes.put( oneNode );
				}
				o.put( "list", listOfNodes );
			} catch ( final JSONException e ) {
				e.printStackTrace();
			}

			out.print( o.toString() );
		}

		out.flush();
		out.close();
	}
}
